#!/bin/sh
#
# Remove a cvs controlled file from both cvs and the SNiFF PDF
#
# RemoveFile.sh <YesNoAsk> <RepositoryRoot> <Sniffaccess Project> <PrivateProjectdir> <catDir> <FileName>
#

test $# != 6 && echo "RemoveFile Error: need 6 parameters, have only $#!" && exit 1

repository_root=$2
project=$3
projectdir=$4
catDir=$5
filename=$6

case $1 in 
YES) ;;
Ask) echo "------------"
	 echo "Remove $filename: are you sure? [y,N or Ctrl-C to cancel]"
	 read sure
	 case $sure in
	   y*|Y*) ;;
	   *) echo "--> NO selected, nothing done."
		  exit 0
		  ;;
	 esac
	 ;;
*)
	echo "Remove $filename: NO selected, nothing done."
	exit 0
	;;
esac

# Assert: at this point the user REALLY wants to remove the file.
filepath=$projectdir/$catDir/$filename
if [ ! -f $filepath ]; then
	echo "Remove error: $filename is shared, please check out first!"
	exit 1
fi
filedir=`dirname $filepath`
if [ ! -f $filedir/CVS/Entries ]; then
	echo "Remove $filename: CVS dir not found, please check out or initialize Project first"
	exit 1
fi
theEntry=`grep "^/$filename/" $filedir/CVS/Entries`
if [ "$theEntry" = "" ]; then
	echo "Remove $filename: CVS/Entries has no info on $filename, please check out again"
	exit 1
fi
result=`$SNIFF_DIR/bin/CVS_DoSniffAccess.sh remove_file "$project" "$catDir" "$filename"`
if [ $? != 0 ]; then
	echo "Unknown sniffaccess error:"
	echo $result
	echo "Aborting..."
	exit 1
fi
echo "Sniffaccess: $result"
case $result in
*editable) 
	echo "Remove $filename: PDF not writable!"
	;;
"")
	# physically remove only if the file is local;
	cd "$filedir"
	if [ -f $filename ]; then
		echo Remove $filename: physically removing $filedir/$filename
		rm -f $filename
	fi
	echo Remove $filename: cvs delete $filename
	cvs delete $filename
	echo Remove $filename: cvs commit -m \"Removed $filename\" $filename
	cvs commit -m "Removed $filename" $filename
	;;
*)
	echo "Remove $filename: Sniffaccess Error $result"
	;;
esac
exit 0
